<?

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/custom/adminnewslettergroupusers.php
#	FUNCTION:		Custom handler for the Newsletter Group Users page
#	AUTHOR:			Cameron Morrow
#	CREATED:		31/08/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Get group names and IDs
$group_names = getAllNewsletterGroupNames();
$group_ids = getAllNewsletterGroupIDs();

# Check group ID passed
$group_id = (@$_GET["group"]) ? (integer) $_GET["group"] : 1;
$group_name = $group_names[findInArray($group_ids, $group_id)];

# Check for Removing
if (@$_POST["removeuser"]) {

	# User ID
	$user_id = $_POST["removeuser"];

	# Get current groups for user
	$user_groups = getRecord("SELECT u_newsletter_groups FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_id = '" . $user_id . "'");

	# If results got
	if (count($user_groups) == 1) {

		# New value
		$new_user_groups = "";

		# Crop
		$user_groups = $user_groups[0]["u_newsletter_groups"];

		# If group was sole entry, new one is empty
		if ($user_groups == (string) $group_id) {

			$new_user_groups = "";

		# Else if it is in the middle of several
		} else if (strpos($user_groups, "," . $group_id . ",") !== false) {

			# Remove item
			$new_user_groups = str_replace("," . $group_id . ",", ",", $user_groups);

		# Else if it is first
		} else if (substr($user_groups, 0, strpos($user_groups, ",") + 1) == $group_id . ",") {

			# Remove first item
			$new_user_groups = substr($user_groups, strlen((string) $group_id) + 1);

		# Else if it is the last
		} else if (substr($user_groups, strlen($user_groups) - strlen((string) $group_id) - 1) == "," . $group_id) {

			# Remove last item
			$new_user_groups = substr($user_groups, 0, strlen($user_groups) - strlen((string) $group_id) - 1);

		}

		# Update SQL
		$sql = "UPDATE " . $PROJECT_DB_TABLES["users"] . " SET u_newsletter_groups = '" . $new_user_groups . "' WHERE u_id = '" . $user_id . "'";

		# Execute
		if (executeSQLQuery($sql)) {
			addMessage("User <em>" . $user_id . "</em> was removed from " . $group_name);

			addLog($user_id . "|" .  $new_user_groups, 24);
		} else {
			addMessage("An error occured and user <em>" . $user_id . "</em> was not removed from " . $group_name, 2);
		}
	}
}

# Check if adding
if (@$_POST["adduser"]) {

	# User ID
	$user_id = $_POST["adduser"];

	# Get current groups for user
	$user_groups = getRecord("SELECT u_newsletter_groups FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_id = '" . $user_id . "'");

	# If results got
	if (count($user_groups) == 1) {

		# New value
		$new_user_groups = "";

		# Crop
		$user_groups = $user_groups[0]["u_newsletter_groups"];

		# If nothing just add
		if ($user_groups == "") {
			$new_user_groups = $group_id;
		} else {
			$new_user_groups = $user_groups . "," . $group_id;
		}

		# Update SQL
		$sql = "UPDATE " . $PROJECT_DB_TABLES["users"] . " SET u_newsletter_groups = '" . $new_user_groups . "' WHERE u_id = '" . $user_id . "'";

		# Execute
		if (executeSQLQuery($sql)) {
			addMessage("User <em>" . $user_id . "</em> was added to " . $group_name);

			addLog($user_id . "|" .  $new_user_groups, 24);
		} else {
			addMessage("An error occured and user <em>" . $user_id . "</em> was not added to " . $group_name, 2);
		}
	}
}


# Get list of Users in current group
$sql = "SELECT u_id, u_fname, u_lname FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_newsletter_groups = '" . $group_id . "' OR u_newsletter_groups LIKE '" . $group_id . ",%' OR u_newsletter_groups LIKE '%," . $group_id . ",%' OR u_newsletter_groups LIKE '%," . $group_id . "'";

# Get record
$users_in_group = getRecord($sql);

# Get User IDs
$user_ids_array = extractColumnFromArray($users_in_group, "u_id");

# Get User Names
$user_names_array = array();
foreach ($users_in_group as $user_details) {
	$user_names_array[] = $user_details["u_fname"] . " " . $user_details["u_lname"] . " (" . $user_details["u_id"] . ")";
}

# Get list of Users NOT in current group
$sql = "SELECT u_id, u_fname, u_lname FROM " . $PROJECT_DB_TABLES["users"] . " WHERE u_newsletter_groups != '" . $group_id . "' AND u_newsletter_groups NOT LIKE '" . $group_id . ",%' AND u_newsletter_groups NOT LIKE '%," . $group_id . ",%' AND u_newsletter_groups NOT LIKE '%," . $group_id . "'";

# Get record
$users_not_in_group = getRecord($sql);

# Get User IDs
$nongroup_user_ids_array = extractColumnFromArray($users_not_in_group, "u_id");

# Get User Names
$nongroup_user_names_array = array();
foreach ($users_not_in_group as $user_details) {
	$nongroup_user_names_array[] = $user_details["u_fname"] . " " . $user_details["u_lname"] . " (" . $user_details["u_id"] . ")";
}

# Search
addContent("<h2>Add User to " . $group_name . "</h2>");
if (count($nongroup_user_ids_array) > 0) {
	addContent("<p>Select a user to add to this group:</p>\n" .
		"<form name=\"useraddform\" id=\"useraddform\" action=\"" . $PAGE_ID . ".php?group=" . $group_id . "\" method=\"post\"><p>Users: " .
		createSelector("adduser", $nongroup_user_names_array, $nongroup_user_ids_array, 0) .
		"<input type=\"submit\" value=\"Go\" /></p>" .
		"</form>");
} else {
	addContent("<p>No users exist who are not in this group.</p>");
}

# Remove
addContent("<h2>Remove User from " . $group_name . "</h2>");
if (count($user_ids_array) > 0) {
	addContent("<p>Select a user to remove from this newsletter group:</p>\n" .
		"<form name=\"userremoveform\" id=\"userremoveform\" action=\"" . $PAGE_ID . ".php?group=" . $group_id . "\" method=\"post\"><p>Users: " .
		createSelector("removeuser", $user_names_array, $user_ids_array, 0) .
		"<input type=\"submit\" value=\"Go\" /></p>" .
		"</form>");
} else {
	addContent("<p>No users exist in this group.</p>");
}

# Close Button
addContent("<div class=\"closebutton\"><input type=\"button\" value=\"Close\" onclick=\"window.close();\" /></div>");

?>